/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.HorizontalScrollView;

/**
 * @author Ganymede
 * 
 */
public class MyHorizontalScrollView extends HorizontalScrollView {

	/**
	 * Position of the last motion event.
	 */
	private float mLastMotionX;
	private float mLastMotionY;

	private int mTouchSlop;
	private int mTouchTimeout;
	private long mTouchTime;

	private long ignoreTouchDowntime = -1;

	/**
	 * @param context
	 */
	public MyHorizontalScrollView(Context context) {
		this(context, null, -1);

	}

	/**
	 * @param context
	 * @param attrs
	 */
	public MyHorizontalScrollView(Context context, AttributeSet attrs) {
		this(context, attrs, -1);

	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		mTouchSlop = ViewConfiguration.getTouchSlop();
		mTouchTimeout = ViewConfiguration.getLongPressTimeout();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.HorizontalScrollView#onInterceptTouchEvent(android.view
	 * .MotionEvent)
	 */

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {

		boolean intercept = super.onInterceptTouchEvent(ev);

		final int action = ev.getAction();
		final float x = ev.getX();
		final float y = ev.getY();
		final long touchdownTime = ev.getEventTime() - mTouchTime;

		switch (action) {
		case MotionEvent.ACTION_MOVE:

			/*
			 * Locally do absolute value. mLastMotionX is set to the x value of
			 * the down event.
			 */
			final int xDiff = (int) Math.abs(x - mLastMotionX);
			final int yDiff = (int) Math.abs(y - mLastMotionY);

			if (ignoreTouchDowntime == mTouchTime
					|| (touchdownTime > mTouchTimeout && xDiff < mTouchSlop && yDiff < mTouchSlop)) {

				intercept = false;
				ignoreTouchDowntime = mTouchTime;

				if (getParent() != null)
					getParent().requestDisallowInterceptTouchEvent(false);
			}
			break;

		case MotionEvent.ACTION_DOWN:
			/* Remember location of down touch */
			mLastMotionX = x;
			mLastMotionY = y;

			mTouchTime = ev.getEventTime();
			break;

		case MotionEvent.ACTION_UP:
			ignoreTouchDowntime = -1;
			break;
		}

		return intercept;
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {

		if (super.onTouchEvent(ev)) {

			final int action = ev.getAction();
			final float x = ev.getX();
			final float y = ev.getY();

			switch (action) {
			case MotionEvent.ACTION_DOWN:
				// Remember where the motion event started
				mLastMotionX = x;
				mLastMotionY = y;
				break;
			}
		}

		return true;
	}

	/**
	 * @return Returns true this HorizontalScrollView can be scrolled
	 */
	private boolean canScroll() {
		View child = getChildAt(0);
		if (child != null) {
			int childWidth = child.getWidth();
			return getWidth() < childWidth + getPaddingLeft() + getPaddingRight();
		}
		return false;
	}

}
